package code.wrtp.packets;

import code.exceptions.BadPduFormatException;
import code.wrtp.FrameControlType;

public class SolicitEnteringReply implements Packet {

	private byte FC;
	private short DA;
	private short SA;
	private short mySuccessorID;

	public SolicitEnteringReply() {

	}

	public SolicitEnteringReply(byte FC, short DA, short SA, short mySuccessorID) {
		this.FC = FC;
		this.DA = DA;
		this.SA = SA;
		this.mySuccessorID = mySuccessorID;
	}

	public byte getFC() {
		return FC;
	}

	public short getDA() {
		return DA;
	}

	public short getSA() {
		return SA;
	}

	public short getMySuccessorID() {
		return mySuccessorID;
	}
	/*Packet format. Today, we don't need RA*/
	public String toString() {
		return FC + ";" + DA + ";" + SA + ";" + mySuccessorID;
	}

	@Override
	public byte[] toBytes() {
		return toString().getBytes();
	}

	@Override
	public void ParseBytes(byte[] pduRaw) throws BadPduFormatException {

		String[] splittedPacket = new String(pduRaw).split(";", 4);
		if (splittedPacket.length != 4) {
			throw new BadPduFormatException("Wrong packet format!");
		}

		FC = Byte.parseByte(splittedPacket[0]);
		if ((FC != FrameControlType.RING_ENTERING_REPLY)
				&& (FC != FrameControlType.VARAL_ENTERING_REPLY)) {
			throw new BadPduFormatException("Wrong packet format!");
		}
		DA = Short.parseShort(splittedPacket[1]);
		SA = Short.parseShort(splittedPacket[2]);
		mySuccessorID = Short.parseShort(splittedPacket[3]);
	}
}
